<?php
	require_once './Tool/DB/SqlDBManager.class.php';
	$sqlDBManager = new SqlDBManager();

	if(!empty($_SESSION["centreName"])){
		$centreName = $_SESSION["centreName"];
	}
	if(!empty($_SESSION["userType"])){
		$userType = $_SESSION["userType"];
	}

	if($userType=="System Admin"){
		$centreId = intval(isset($_POST['CentreSubmit']) ? $_POST['ddlCentre'] : '' );

		if($centreId==0){
			$sql = "SELECT ar.NRIC, c.CustomerName, ar.SignInTimeStamp,  ar.SignOutTimeStamp, c.Daycare
				FROM attendancerecord ar, client c
				WHERE DATE( SignInTimeStamp ) = CURDATE( ) 
				AND ar.NRIC = c.NRIC";
			$res=$sqlDBManager->queryRows($sql);
		}else{
			$sql = "SELECT ar.NRIC, c.CustomerName, ar.SignInTimeStamp,  ar.SignOutTimeStamp, c.Daycare
				FROM attendancerecord ar, client c
				WHERE DATE( SignInTimeStamp ) = CURDATE( ) 
				AND ar.NRIC = c.NRIC AND ar.CentreId=?";
			$parameters=array($centreId);
			$res=$sqlDBManager->queryRows($sql,$parameters);
		}
		
		echo "<table class='tablereport' id='tblAttendance' border='0'  valign='top'>
			<tr >
			<td>NRIC</td>
			<td>Name</td>
			<td>Date</td>
			<td>Time In</td>
			<td>Time Out</td>
			<td>Day-Care</td>
			<td>Sign Out
			<form method='POST'><input type='submit' button class='btn' name='signoutall'value='Sign Out All' size='10' maxlength='9'/>
			</form>
			</td>
			</tr>";

		$count=0;

		if(!empty($res)){
			for($i=0;$i<count($res);$i++){
				 	echo"<tr><td>".$res[$i]['NRIC']."</td >
						<td>".$res[$i]['CustomerName']."</td>
						<td>".$today = date('j F, Y')."</td>";
					$signin = strtotime($res[$i]['SignInTimeStamp']);
					$signintime = date('H:i:s', $signin);
					echo"<td>".$signintime."</td>";
					if($res[$i]['SignOutTimeStamp']=='0000-00-00 00:00:00'|| $res[$i]['SignOutTimeStamp']==NULL){
						echo "<td></td>
							<td style='text-align:center;vertical-align:middle'>".$res[$i]['Daycare']."</td>
							<td style='text-align:center;vertical-align:middle'>
							<form method='POST' >
							<button type='submit'  button class='btn'name='btnSO' value='{$res[$i]['NRIC']}'>
				  			Sign Out
							</button></form>
							</td>";
					}else{ 
						$signout = strtotime($res[$i]['SignOutTimeStamp']);
						$signouttime = date('H:i:s', $signout);
						echo"<td>".$signouttime."</td>
							<td style='text-align:center;vertical-align:middle'>".$res[$i]['Daycare']."</td>
							<td style='text-align:center;vertical-align:middle'></td>";
					}
					
					echo"</tr>";
					$count++;
				}	
		}
		echo "</table>";
	}else{
		$sql="SELECT CentreId FROM centre WHERE CentreName = ?";
		$parameters=array($centreName);
		$res=$sqlDBManager->queryRow($sql,$parameters);

		if(!empty($res)){
			$centreId=$res['CentreId'];
		}

		$sql="SELECT ar.NRIC, c.CustomerName, ar.SignInTimeStamp, ar.SignOutTimeStamp, c.Daycare
			FROM attendancerecord ar, client c
			WHERE DATE( SignInTimeStamp ) = CURDATE( ) 
			AND ar.NRIC = c.NRIC
			AND ar.CentreId=?";

		$parameters=array($centreId);
		$res=$sqlDBManager->queryRows($sql,$parameters);

		echo "<table class='tablereport' id='tblAttendance' border='0'  valign='top'>
			<tr>
			<td>NRIC</td>
			<td>Name</td>
			<td>Date</td>
			<td>Time In</td>
			<td>Time Out</td>
			<td>Day-Care</td>
			<td>Sign Out <form method='POST'><input type='submit' button class='btn' name='signoutall'value='Sign Out All' size='10' maxlength='9'/></form></td>
			</tr>";

		$count=0;

		if(!empty($res)){
			for($i=0;$i<count($res);$i++){
				echo"<tr><td>".$res[$i]['NRIC']."</td>
			    	<td>".$res[$i]['CustomerName']."</td>
					<td>".$today = date('j F, Y')."</td>";
				$signin = strtotime($res[$i]['SignInTimeStamp']);
				$signintime = date('H:i:s', $signin);
				echo"<td>".$signintime."</td>";
				if($res[$i]['SignOutTimeStamp']=='0000-00-00 00:00:00'|| $res[$i]['SignOutTimeStamp']==NULL){
					echo "<td></td>
						<td style='text-align:center;vertical-align:middle'>".$res[$i]['Daycare']."</td>
						<td style='text-align:center;vertical-align:middle'><form method='POST'>
						<button type='submit' name='btnSO' value='{$res[$i]['NRIC']}'>
						Sign Out
						</button></form>
						</td>";

				}else{ 
					$signout = strtotime($res[$i]['SignOutTimeStamp']);
					$signouttime = date('H:i:s', $signout);
					echo"<td>".$signouttime."</td>
						<td style='text-align:center;vertical-align:middle'>".$res[$i]['Daycare']."</td>
						<td style='text-align:center;vertical-align:middle'></td>";
				}
				echo "</tr>";
				$count++;
			}
		}
		echo "</table>";
	}
	
	//close connection
	$sqlDBManager->close_connect();
?>